#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : li.py
@Author  : Link
@Time    : 2022/5/21 10:13
@Mark    : 
"""

from typing import Dict, Union, List, Tuple

import pandas as pd
from PySide2.QtCore import QObject, Signal


class Li(QObject):
    message = Signal(str)

    df = None  # type:pd.DataFrame
    df_group = None  # type:Dict[str,pd.DataFrame]

    front_df = None  # type:pd.DataFrame
    front_df_group = None  # type:Dict[str,pd.DataFrame]

    df_signal = Signal()  # 用来触发df改变的信号
    front_df_signal = Signal()  # 用来触发front_df改变的信号

    chart_df = None  # type:pd.DataFrame # 这个df就是简简单单的用来绘图, 基于front_df来绘图
    chart_df_signal = Signal()  # 用来触发chart_df改变的信号

    full_chart_signal = Signal()

    limit = 1
    rig = 5

    @property
    def dt(self):
        return self.front_df

    @dt.setter
    def dt(self, new_df: pd.DataFrame):
        if not isinstance(new_df, pd.DataFrame):
            print(f"Error, 必须要赋值和dt一样的数据！···, 错误类型{type(new_df)}")
            return
        self.set_new_front_df(new_df)

    def set_new_df(self, df: pd.DataFrame):
        self.df = df
        self.df_signal.emit()

    def set_new_front_df(self, df: pd.DataFrame, emit=True):
        """ 使用copy更加安全 """
        self.front_df = df.copy()
        if emit:
            self.front_df_signal.emit()
        self.chart_df = df.copy()
        self.full_chart_signal.emit()

    def set_new_chart_df(self, df: pd.DataFrame):
        self.chart_df = df.copy()
        self.chart_df_signal.emit()

    def refresh(self):
        self.front_df_signal.emit()
        self.refresh_chart()
        return "刷新界面@"

    def refresh_chart(self):
        self.chart_df = self.front_df.copy()
        self.full_chart_signal.emit()

    # def restore(self):
    # """不支持~@太麻烦, 数据会因为Group而改变"""
    #     self.set_new_front_df(self.df)
